//package com.qijian.plug.capacity.cost;
//
//import com.qijian.finance.domain.Cost;
//import com.qijian.finance.domain.enums.CostTypeEnum;
//import com.qijian.maindata.domain.*;
//import com.qijian.maindata.domain.vo.OutVo;
//import com.qijian.maindata.service.ILineProcessService;
//import com.qijian.maindata.service.IProcessOrderService;
//import com.qijian.maindata.service.IProjectService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Component
//@RequiredArgsConstructor
//public class MainOutCost extends CostPlugWrapper<Out.Cost> {
//
//    private final IProcessOrderService processOrderService;
//    private final IProjectService projectService;
//
//    private final ILineProcessService lineProcessService;
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void doSomething(List<Object> args) {
//        super.doSomething(args);
//    }
//
//    @Override
//    protected boolean condition(List<Object> args) {
//        OutVo outVo = (OutVo) args.get(0);
//        return outVo.getStatus() == 4;
//    }
//
//    @Override
//    protected void operation(List<Object> args) {
//
//        OutVo outVo = (OutVo) args.get(0);
//        List<OutDetails> outDetails = outVo.getDetails();
//        //获取所有工单
//        Map<Long, ProcessOrder> ordersMap = processOrderService.listByIds(outDetails.stream().map(OutDetails::getWorkId).collect(Collectors.toSet()))
//                .stream().collect(Collectors.toMap(ProcessOrder::getWorkId, a -> a));
//        //工序分类
//        Map<Long, String> processType = lineProcessService.listByIds(ordersMap.values().stream().map(ProcessOrder::getProcessId).collect(Collectors.toSet()))
//                .stream().collect(Collectors.toMap(LineProcess::getProcessId, LineProcess::getProcessTypeName));
//
//        //获取开工令机台号
//        Map<Long, String> projectMap = projectService.listByIds(ordersMap.values().stream().map(ProcessOrder::getProjectId).collect(Collectors.toSet())).stream()
//                .collect(Collectors.toMap(Project::getProjectId, a -> String.format("%s(%s#)", a.getProjectNo(), a.getMachineNo())));
//
//        //历史top成本数据
//        Map<String, Cost> topCostMap = super.costService.getTopHistoryDataMap(projectMap.values().stream()
//                .filter(Objects::nonNull).collect(Collectors.toSet()));
//        //历史父类成本数据
//        Map<String, Cost> parentCostMap = super.costService.getHighHistoryDataMap(
//                projectMap.values().stream()
//                        .filter(Objects::nonNull).collect(Collectors.toSet()), CostTypeEnum.MAIN_OUT);
//        //历史子类成本数据
//        Set<String[]> childOld = ordersMap.values().stream().map(o -> new String[]{projectMap.get(o.getProjectId()), processType.get(o.getProcessId())}).collect(Collectors.toSet());
//        Map<String, Cost> childCostMap = super.costService.getChildHistoryDataMap(childOld);
//
//        outDetails.forEach(o -> {
//            ProcessOrder po = ordersMap.get(o.getWorkId());
//            String projectNo = projectMap.get(po.getProjectId());
//
//            Cost top = getTopCost(topCostMap, projectNo);
//            Cost high = getHighCost(parentCostMap, projectNo, CostTypeEnum.MAIN_OUT, top);
//            Cost child = getChildCost(childCostMap, projectNo, processType.get(po.getProcessId()), high);
//
//            BigDecimal temp = o.getWorkCost().multiply(new BigDecimal(o.getWorkNumber()).divide(new BigDecimal(po.getWorkNumber()), 6, BigDecimal.ROUND_HALF_UP));
//            child.setActualCost(child.getActualCost().add(temp));
//            high.setActualCost(high.getActualCost().add(temp));
//            top.setActualCost(top.getActualCost().add(temp));
//        });
//        //合并所有数据集
//        List<Cost> saveOrUpdateList = new ArrayList<>
//                (topCostMap.size() + parentCostMap.size() + childCostMap.size());
//        saveOrUpdateList.addAll(topCostMap.values());
//        saveOrUpdateList.addAll(parentCostMap.values());
//        saveOrUpdateList.addAll(childCostMap.values());
//        super.saveOrUpdateBatch(saveOrUpdateList);
//
//    }
//
//}
